#!/bin/sh

########################################################################
# Test: Script to invoke Periscope using Sick Beard's Parameters
# It should properly download subtitles for each finished / succesfully
# processed download. Well, in fact it does.
#
# Alvaro "Effenberg" Leal <AlvaroLeal.Backup@GMail.com>, 2011
# Get the latest code at http://code.google.com/p/call-periscope/
########################################################################

#SET THIS OR IT WILL NOT WORK
PERISCOPE_BIN=/nas/public/daemons/periscope/periscope
PERISCOPE_LANGS='-f -l en -l pt-br'
CALL_PERISCOPE_LOG=/nas/public/daemons/sickbeard/CallPeriscope.log

#########################################################################

#Other Args / Variables. Leave it alone.
PROCESSED_EPISODE_FULL_PATH=$1
PROCESSED_EPISODE_PATH=`dirname "${1}"`
PROCESSED_EPISODE_FILE_NAME=`basename "${1}"`
ORIGINAL_EPISODE_NAME=`basename "${2}"`
PERISCOPE_OPTS="${PERISCOPE_LANGS} -q '$ORIGINAL_EPISODE_NAME'"

#Debug - too see if its beeing invoked at all
date >> /nas/public/daemons/sickbeard/CallPeriscope.log 
echo Pre-invoking periscope -v to check that it is ok...
echo Periscope version: `$PERISCOPE_BIN --version` >> $CALL_PERISCOPE_LOG

#Check what we are gettiong from Sick Beard
echo CallPeriscope.sh Received arg 1: $1 >> $CALL_PERISCOPE_LOG
echo CallPeriscope.sh Received arg 2: $2 >> $CALL_PERISCOPE_LOG
echo CallPeriscope.sh Received arg 3: $3 >> $CALL_PERISCOPE_LOG
echo CallPeriscope.sh Received arg 4: $4 >> $CALL_PERISCOPE_LOG
echo CallPeriscope.sh Received arg 5: $5 >> $CALL_PERISCOPE_LOG

#Check if we processed it right and got what we needed
echo Processed args are: >> $CALL_PERISCOPE_LOG
echo Arg 1: $PROCESSED_EPISODE_FULL_PATH >> $CALL_PERISCOPE_LOG
echo Arg 2: $ORIGINAL_EPISODE_NAME >> $CALL_PERISCOPE_LOG
echo Processed Episode Path is $PROCESSED_EPISODE_PATH >> $CALL_PERISCOPE_LOG
echo Processed Episode File Name is $PROCESSED_EPISODE_FILE_NAME >> $CALL_PERISCOPE_LOG

#Now lets finally do something
echo Changing Directory to provided episode path... >> $CALL_PERISCOPE_LOG
echo From `pwd` >> $CALL_PERISCOPE_LOG
cd "${PROCESSED_EPISODE_PATH}"
echo To `pwd` >> $CALL_PERISCOPE_LOG
echo Invoking Periscope to original episode name $ORIGINAL_EPISODE_NAME >> $CALL_PERISCOPE_LOG
echo The final command line is: >> $CALL_PERISCOPE_LOG
echo $PERISCOPE_BIN $PERISCOPE_OPTS $PROCESSED_EPISODE_FILE_NAME >> $CALL_PERISCOPE_LOG
echo Invoking it now: >> $CALL_PERISCOPE_LOG

#Call Pesriscope with proper command line args - Now its not up to us
`$PERISCOPE_BIN $PERISCOPE_OPTS $PROCESSED_EPISODE_FILE_NAME >> $CALL_PERISCOPE_LOG`

#And exit
echo Exiting... >> $CALL_PERISCOPE_LOG
exit 0

